﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace baseClass.controls
{
    public partial class emLeaveDetail : common.control.baseUserControl
    {
        private BindingSource myDataSource = null;
        public emLeaveDetail()
        {
            InitializeComponent();
            SetMaxLength();
            leaveTypeCb.SelectFirstIfNull();
            planDateEd.ForeColor = common.settings.sysColorDisableFG;
            planDateEd.BackColor = common.settings.sysColorDisableBG;
        }

        public void ReLoad()
        {
            UpdatePlanRestartDate();
        }

        public void SetDataSource(System.Windows.Forms.BindingSource dataSrc)
        {
            this.myDataSource = dataSrc;
            data.baseDS.emLeaveDataTable tbl = (data.baseDS.emLeaveDataTable)dataSrc.DataSource;
            this.frDateEd.DataBindings.Clear();
            this.frDateEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.frDateColumn.ColumnName, true));
            
            this.durationEd.DataBindings.Clear();
            this.durationEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.durationColumn.ColumnName, true));
            
            this.restartDateEd.DataBindings.Clear();
            this.restartDateEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.reStartDateColumn.ColumnName, true));

            this.leaveTypeCb.DataBindings.Clear();
            this.leaveTypeCb.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", dataSrc, tbl.typeColumn.ColumnName, true));

            this.noteEd.DataBindings.Clear();
            this.noteEd.DataBindings.Add(new System.Windows.Forms.Binding("Text", dataSrc, tbl.notesColumn.ColumnName, true));
        }
        private void SetMaxLength()
        {
            data.baseDS.emEducationDataTable tbl = new data.baseDS.emEducationDataTable();
            noteEd.MaxLength = tbl.notesColumn.MaxLength;
        }
        public virtual void Init() 
        {
            leaveTypeCb.LoadData();
        }

        public virtual bool CheckData()
        {
            if (this.myDataSource.Current == null) return true;
            bool retVal = true;
            ClearNotifyError();
            if (frDateEd.myDateTime == common.Consts.constNullDate) 
            {
                NotifyError(frDateLbl);
                retVal = false;
            }
            if (leaveTypeCb.myValue == "") 
            {
                NotifyError(leaveTypeLbl);
                retVal = false;
            }
            return retVal;
        }
        public virtual void LockEdit(bool state)
        {
            if (myDataSource.Current == null) state = true;
            frDateEd.ReadOnly = state;
            durationEd.ReadOnly = state;
            restartDateEd.ReadOnly = state;
            leaveTypeCb.Enabled = !state;
            noteEd.ReadOnly = state;
        }
        public virtual void SetFocus()
        {
            this.Focus();
            frDateEd.Focus();
        }
        private void UpdatePlanRestartDate()
        {
            if (myDataSource.Current == null)
            {
                planDateEd.myDateTime = common.Consts.constNullDate;
                return;
            }
            data.baseDS.emLeaveRow row = (data.baseDS.emLeaveRow)((DataRowView)myDataSource.Current).Row; 
            planDateEd.myDateTime = application.sysLibs.GetWorkDayDate(row.frDate,(int)durationEd.Value);
        }

        private void durationEd_ValueChanged(object sender, EventArgs e)
        {
            UpdatePlanRestartDate();
        }

        private void frDateEd_Validated(object sender, EventArgs e)
        {
            UpdatePlanRestartDate();
        }
    }
}
